package org.lanqiao.oa.visitor.impl;


import org.lanqiao.oa.dao.PermissionDao;
import org.lanqiao.oa.dao.RolePermissionRelationDao;
import org.lanqiao.oa.dto.PermissionDTO;
import org.lanqiao.oa.visitor.PermissionVisitor;
import org.springframework.stereotype.Repository;


@Repository
public class CheckRolePermissionRelationVisitor extends AbstractVisitorClass implements PermissionVisitor {

    //用于操作数据库
    private PermissionDao permissionDao;
    private RolePermissionRelationDao rolePermissionRelationDao;
    //标记权限的状态
    private Boolean status = false;

    @Override
    PermissionDao permissionDao() {
        return permissionDao;
    }

    public CheckRolePermissionRelationVisitor(PermissionDao permissionDao, RolePermissionRelationDao rolePermissionRelationDao) {
        this.permissionDao = permissionDao;
        this.rolePermissionRelationDao = rolePermissionRelationDao;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    /**
     * 验证权限是否被使用(查询子权限，查询权限与角色的分配情况)
     * @param permissionDto
     */
    @Override
    public void visit(PermissionDTO permissionDto) {

        //遍历权限树，判断权限是否被使用
        Long permissionUseCount = rolePermissionRelationDao.getPermissionUseCount(permissionDto.getId());
        if(permissionUseCount !=null && permissionUseCount>0 ){
            //被使用
            this.status = true;
            //System.out.println(permissionDto.getName()+"被使用");
        }/*else {
            //System.out.println(permissionDto.getName()+"未被使用");
        }*/

    }



}
